<?php
/**
 * WQPHP封装工具类，仅供学习研究使用，禁止任何形式的转载发布
 * User: Jessica@aweb.wang
 * Date: 2023/3/10 14:50
 * Email: <1194323013@qq.com>
 *                            _ooOoo_
 *                           o8888888o
 *                           88" . "88
 *                           (| -_- |)
 *                           O\  =  /O
 *                        ____/`---'\____
 *                      .'  \\|     |//  `.
 *                     /  \\|||  :  |||//  \
 *                    /  _||||| -:- |||||-  \
 *                    |   | \\\  -  /// |   |
 *                    | \_|  ''\---/''  |   |
 *                    \  .-\__  `-`  ___/-. /
 *                  ___`. .'  /--.--\  `. . __
 *               ."" '<  `.___\_<|>_/___.'  >'"".
 *              | | :  `- \`.;`\ _ /`;.`/ - ` : | |
 *              \  \ `-.   \_ __\ /__ _/   .-` /  /
 *         ======`-.____`-.___\_____/___.-`____.-'======
 *                            `=---='
 *        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 *                      Buddha Bless, No Bug !
 **/

namespace app\common\utils;
use think\facade\Config;
/**
 * Class CurlUtils  请求类
 * @package app\common\utils
 */
class RequestUtils
{

    /**
     * 模拟POST请求
     * @param $url 请求链接
     * @param $data 请求数据
     */
    public static function MakePostRequest($url,$data)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        $output = curl_exec($ch);
        curl_close($ch);
        if($output){
            return $output;
        }
        else{
            $error=curl_errno($ch);
            return $error;
        }
    }

    /**
     * 模拟GET请求
     * @param $url 请求链接
     */
    public static function MakeGetRequest($url,$type=1){
        $curl = curl_init();
        curl_setopt($curl,CURLOPT_TIMEOUT,5000);
        curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,false);
        curl_setopt($curl,CURLOPT_SSL_VERIFYHOST,false);
        curl_setopt($curl,CURLOPT_USERAGENT,$_SERVER['HTTP_USER_AGENT']);
        curl_setopt($curl,CURLOPT_URL,$url);
        if($type == 1){
            curl_setopt($curl,CURLOPT_RETURNTRANSFER,true);
        }
        $res = curl_exec($curl);
        if($res){
            curl_close($curl);
            return $res;
        }else {
            $error = curl_errno($curl);
            curl_close($curl);
            return $error;
        }
    }

    /**
     * @param $params 参数数组
     * @param $secret 密钥
     */
    public static function makeSign($params,$secret){
        ksort($params);
        $signstr='';
        foreach ($params as$key=> $value) {
            $signstr=$signstr.'&'.$key.'='.$value;
        }
        $signstr=substr($signstr,1,strlen($signstr));
        $sign=md5($signstr.$secret);
        return $sign;
    }
}